package com.xg.frame.utils;

import com.xg.frame.entity.ConstantCode;
import com.xg.frame.exception.SqException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 参数校验格式化工具类
 * Created by dehuisun on 2020/7/28
 */
public class VerifyParamsUtils {

    static SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 日期格式化
     * @param strDate
     * @return
     */
    public static String formatDate(String strDate){
        String s=null;
        try {
            Date date = sdfDate.parse(strDate);
            s = sdfDate.format(date);
        } catch (Exception e) {
            throw new SqException(ConstantCode.NOT_VERIFY.getCode(), "日期格式有误");
        }finally {
            return s;
        }
    }

    /**
     * 日期格式化加天数
     * @param strDate
     * @param days
     * @return
     */
    public static String formatDateAddDays(String strDate,int days){
        String s=null;
        try {
            Calendar calendar = new GregorianCalendar();
            Date date = sdfDate.parse(strDate);
            calendar.setTime(date);
            calendar.add(Calendar.DATE,days);
            date=calendar.getTime();
            s = sdfDate.format(date);
        } catch (Exception e) {
            throw new SqException(ConstantCode.NOT_VERIFY.getCode(), "日期格式有误");
        }finally {
            return s;
        }
    }
}
